﻿using System;

namespace Manoso.Data.FluentMap
{
    public interface IMapProperty
    {
        /// <summary>
        /// The declaring type of the property.
        /// </summary>
        string HostAlias { get; set; }

        /// <summary>
        /// Type of the property.
        /// </summary>
        Type PropertyType { get; set; }

        /// <summary>
        /// Property name.
        /// </summary>
        string PropertyName { get; set; }

        /// <summary>
        /// Property value.
        /// </summary>
        object Value { get; set; }

        /// <summary>
        /// Declaring object value.
        /// </summary>
        object Host { get; set; }

        /// <summary>
        /// IMapColumn object that defines the column.
        /// </summary>
        IMapColumn Column { get; set; }

        /// <summary>
        /// Action to load value from declaring object.
        /// </summary>
        Action<object> Load { get; set; }

        /// <summary>
        /// Action to set value to declaring object.
        /// </summary>
        Action<object, object> Set { get; set; }

        /// <summary>
        /// Return a clone of the current object.
        /// </summary>
        IMapProperty Copy { get; }
    }

    internal class MapProperty : IMapProperty
    {
        public string HostAlias { get; set; }
        public Type PropertyType { get; set; }
        public string PropertyName { get; set; }
        public object Host { get; set; }
        public IMapColumn Column { get; set; }

        public Action<object> Load { get; set; }
        public Action<object, object> Set { get; set; }

        public IMapProperty Copy
        {
            get { return MemberwiseClone() as IMapProperty; }
        }

        public object Value 
        {
            get { return Column.Value; }
            set { Column.Value = value; }
        }
    }
}
